#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time
import datetime
import sqlite3
import os

# Unit:Minute
DELAY_TIME = 5
# Time zone
TIME_ZONE = 8
TIME_ZONE_TIME = TIME_ZONE * 3600
#Work time
WORK_HOUR = 9
WORK_MINUTE = 0
WORK_TIME = WORK_HOUR * 3600 + WORK_MINUTE * 60
#Half day work time
HALF_WORK_HOUR = 4
HALF_WORK_MINUTE = 0
HALF_WORK_TIME = HALF_WORK_HOUR * 3600 + HALF_WORK_MINUTE * 60
# Earliest work time
EARLIEST_WORK_HOUR = 7
EARLIEST_WORK_MINUTE = 30
EARLIEST_WORK_TIME = EARLIEST_WORK_HOUR * 3600 + EARLIEST_WORK_MINUTE * 60 
# Latest work time
LATEST_WORK_HOUR = 10
LATEST_WORK_MINUTE = 0
LATEST_WORK_TIME = LATEST_WORK_HOUR * 3600 + LATEST_WORK_MINUTE * 60
#Half day earliest work time
HALF_DAY_EARLIEST_WORK_HOUR = 13
HALF_DAY_EARLIEST_WORK_MINUTE = 0
HALF_DAY_EARLIEST_WORK_TIME = HALF_DAY_EARLIEST_WORK_HOUR * 3600 + HALF_DAY_EARLIEST_WORK_MINUTE * 60

# 算出下班的绝对时间
def cal_off_work_time_and_set(cur_time, cur_utc_today_time, first_utc_today_time):
    if first_utc_today_time < EARLIEST_WORK_TIME:
        target_time_hour = EARLIEST_WORK_HOUR + WORK_HOUR
        target_time_minute = EARLIEST_WORK_MINUTE + DELAY_TIME
    elif EARLIEST_WORK_TIME < first_utc_today_time < LATEST_WORK_TIME:
        target_time_hour = cur_time.hour + WORK_HOUR
        target_time_minute = cur_time.minute + DELAY_TIME
    elif LATEST_WORK_TIME < first_utc_today_time < HALF_DAY_EARLIEST_WORK_TIME:
        target_time_hour = HALF_DAY_EARLIEST_WORK_HOUR + HALF_WORK_HOUR
        target_time_minute = HALF_DAY_EARLIEST_WORK_MINUTE + DELAY_TIME
    else:
        target_time_hour = cur_time.hour + HALF_WORK_HOUR
        target_time_minute = cur_time.minute + DELAY_TIME
    target_setting_time = target_time_hour * 3600 + target_time_minute * 60
    diff_time = target_setting_time - cur_utc_today_time
    return diff_time
    

cur_time = datetime.datetime.now()
cur_utc_time = int(time.mktime(cur_time.timetuple()))
cur_utc_today_time = (cur_utc_time + TIME_ZONE_TIME) % 86400
# cur_time = time.strptime('2020-08-11 14:09:17', "%Y-%m-%d %H:%M:%S")
# cur_utc_time = int(time.mktime(cur_time))
# cur_time = datetime.datetime.fromtimestamp(cur_utc_time)

conn = sqlite3.connect('attendance_table.db')
comm = conn.cursor()
cursor = comm.execute("select * from attendance_table order by id desc limit 0,1")
last_utc_time = cursor.fetchall()
if last_utc_time:
    last_time = datetime.datetime.fromtimestamp(last_utc_time[0][1])
    diff_day_utc_time = int((cur_utc_time + TIME_ZONE_TIME) / 86400) - int((last_utc_time[0][1] + TIME_ZONE_TIME) / 86400)
    last_utc_today_time = (last_utc_time[0][1] + TIME_ZONE_TIME) % 86400
    if diff_day_utc_time > 0:
        comm.execute("insert into attendance_table (TIME) VALUES (" + str(cur_utc_time) + ")")
        remain_time = cal_off_work_time_and_set(cur_time, cur_utc_today_time, cur_utc_today_time)
    else:
        remain_time = cal_off_work_time_and_set(last_time, cur_utc_today_time, last_utc_today_time)
else:
    comm.execute("insert into attendance_table (TIME) VALUES (" + str(cur_utc_time) + ")")
    remain_time = cal_off_work_time_and_set(cur_time, cur_utc_today_time, cur_utc_today_time)

conn.commit()
conn.close()
os.popen("shutdown /s /t " + str(remain_time))